<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/8 0008
 * Time: 下午 4:47
 */
namespace app\admin\controller;

use think\Exception;
use think\Request;

class Storecashier extends Base{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = 'store_cashier';
        $this->defaultOrder = 'create_date DESC';
        $defaultWhere = $this->getDefaultWhere();
        $this->defaultWhere = $defaultWhere;
    }

    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        // Reset filter
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'] + 1;
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }
            $data = $model->where($where)->limit($start,$length)->order($order)->select();
            if($data){
                foreach ($data as &$v){
                    $store = db('store')->where('id',$v['store_id'])->field('name')->find();
                    $storename = '';
                    if($store){
                        $storename = $store['name'];
                    }
                    $v['storename'] = $storename;
                }
            }
            $count = $model->where($where)->count();
            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return $this->fetch('list');

    }

    public function add(Request $request){
        $store = db('store')->where(true)->field('id,name')->select();
        return $this->fetch('',[
            'store' => $store
        ]);
    }

    public function savepost(Request $request){
        if($request->isPost()){
            $param = $request->param();
            if(!empty($param['serial_number']) && !empty($param['store_id'])){
                $store_cashier = db('store_cashier')->where('serial_number',$param['serial_number'])->find();
                if($store_cashier){
                    return $this->error('此收银机已绑定店铺，请先删除');
                }
                $model = new \app\admin\model\StoreCashier();
                $model->serial_number = $param['serial_number'];
                $model->store_id = $param['store_id'];
                try{
                    $model->save();
                    return $this->success('绑定成功');
                }catch (Exception $ex){
                    return $this->error($ex->getMessage());
                }
            }else{
                return $this->error('参数错误');
            }
        }else{
            return $this->error('非法请求');
        }
    }
}